package org.dfzt.modules.online.cgform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dfzt.common.system.service.ISysBaseAPI;
import org.dfzt.common.util.oConvertUtils;
import org.dfzt.modules.online.cgform.link.LinkDown;
import org.dfzt.modules.online.cgform.entity.OnlCgformButton;
import org.dfzt.modules.online.cgform.entity.OnlCgformEnhanceJs;
import org.dfzt.modules.online.cgform.entity.OnlCgformField;
import org.dfzt.modules.online.cgform.entity.OnlCgformHead;
import org.dfzt.modules.online.cgform.model.HrefSlots;
import org.dfzt.modules.online.cgform.model.OnlColumn;
import org.dfzt.modules.online.cgform.model.OnlComplexModel;
import org.dfzt.modules.online.cgform.model.OnlForeignKey;
import org.dfzt.modules.online.cgform.model.ScopedSlots;
import org.dfzt.modules.online.cgform.model.Field;
import org.dfzt.modules.online.cgform.service.IOnlCgformFieldService;
import org.dfzt.modules.online.cgform.service.IOnlCgformHeadService;
import org.dfzt.modules.online.cgform.service.IOnlineService;
import org.dfzt.modules.online.cgform.util.EnhanceJsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("onlineService")
public class OnlineServiceImpl
        implements IOnlineService {
    @Autowired
    private IOnlCgformFieldService onlCgformFieldService;
    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Override
    public OnlComplexModel queryOnlineConfig(OnlCgformHead head) {
        String str = head.getId();

        List<OnlCgformField> list1 = a(str);

        List list2 = this.onlCgformFieldService.selectOnlineHideColumns(head.getTableName());

        ArrayList arrayList1 = new ArrayList();

        HashMap hashMap = new HashMap();

        ArrayList arrayList2 = new ArrayList();

        ArrayList arrayList3 = new ArrayList();
        ArrayList arrayList4 = new ArrayList();

        for (OnlCgformField onlCgformField : list1) {
            String str1 = onlCgformField.getDbFieldName();


            String str2 = onlCgformField.getMainTable();
            String str3 = onlCgformField.getMainField();
            if (oConvertUtils.isNotEmpty(str3) && oConvertUtils.isNotEmpty(str2)) {
                OnlForeignKey d = new OnlForeignKey(str1, str3);
                arrayList3.add(d);
            }


            if (1 != onlCgformField.getIsShowList().intValue()) {
                continue;
            }


            if ("id".equals(str1)) {
                continue;
            }

            if (list2.contains(str1)) {
                continue;
            }

            if (arrayList4.contains(str1)) {
                continue;
            }

            OnlColumn b = new OnlColumn(onlCgformField.getDbFieldTxt(), str1);
            String str4 = onlCgformField.getDictField();
            String str5 = onlCgformField.getFieldShowType();

            if (oConvertUtils.isNotEmpty(str4) && !"popup".equals(str5)) {
                List list = new ArrayList();

                if (oConvertUtils.isNotEmpty(onlCgformField.getDictTable())) {
                    List list4 = this.sysBaseAPI.queryTableDictItemsByCode(onlCgformField.getDictTable(), onlCgformField.getDictText(), str4);
                } else if (oConvertUtils.isNotEmpty(onlCgformField.getDictField())) {
                    list = this.sysBaseAPI.queryDictItemsByCode(str4);
                }
                hashMap.put(str1, list);
                b.setCustomRender(str1);
            }

            if ("link_down".equals(str5)) {
                String str6 = onlCgformField.getDictTable();
                LinkDown a = JSONObject.parseObject(str6, LinkDown.class);
                List list = this.sysBaseAPI.queryTableDictItemsByCode(a.getTable(), a.getTxt(), a.getKey());
                hashMap.put(str1, list);
                b.setCustomRender(str1);
                arrayList1.add(b);

                String str7 = a.getLinkField();
                a(list1, arrayList4, arrayList1, str1, str7);
            }

            if ("sel_tree".equals(str5)) {

                String[] arrayOfString = onlCgformField.getDictText().split(",");
                List list = this.sysBaseAPI.queryTableDictItemsByCode(onlCgformField.getDictTable(), arrayOfString[2], arrayOfString[0]);
                hashMap.put(str1, list);
                b.setCustomRender(str1);
            }

            if ("cat_tree".equals(str5)) {

                String str6 = onlCgformField.getDictText();
                if (oConvertUtils.isEmpty(str6)) {

                    String str7 = org.dfzt.modules.online.cgform.util.b.e(onlCgformField.getDictField());
                    List list = this.sysBaseAPI.queryFilterTableDictInfo("SYS_CATEGORY", "NAME", "ID", str7);
                    hashMap.put(str1, list);
                    b.setCustomRender(str1);
                } else {

                    b.setCustomRender("_replace_text_" + str6);
                }
            }


            if ("sel_depart".equals(str5)) {
                List list = this.sysBaseAPI.queryAllDepartBackDictModel();
                hashMap.put(str1, list);
                b.setCustomRender(str1);
            }


            if ("sel_user".equals(onlCgformField.getFieldShowType())) {
                List list = this.sysBaseAPI.queryTableDictItemsByCode("SYS_USER", "REALNAME", "USERNAME");
                hashMap.put(str1, list);
                b.setCustomRender(str1);
            }


            if (str5.indexOf("file") >= 0) {
                b.setScopedSlots(new ScopedSlots("fileSlot"));
            } else if (str5.indexOf("image") >= 0) {
                b.setScopedSlots(new ScopedSlots("imgSlot"));
            } else if (str5.indexOf("editor") >= 0) {
                b.setScopedSlots(new ScopedSlots("htmlSlot"));
            } else if (str5.equals("date")) {
                b.setScopedSlots(new ScopedSlots("dateSlot"));
            } else if (str5.equals("pca")) {
                b.setScopedSlots(new ScopedSlots("pcaSlot"));
            }

            if (StringUtils.isNotBlank(onlCgformField.getFieldHref())) {
                String str6 = "fieldHref_" + str1;

                b.setHrefSlotName(str6);
                arrayList2.add(new HrefSlots(str6, onlCgformField.getFieldHref()));
            }

            if ("1".equals(onlCgformField.getSortFlag())) {
                b.setSorter(true);
            }
            if (!"link_down".equals(str5)) {
                arrayList1.add(b);
            }
        }


        OnlComplexModel c = new OnlComplexModel();
        c.setCode(str);
        c.setTableType(head.getTableType());
        c.setFormTemplate(head.getFormTemplate());
        c.setDescription(head.getTableTxt());
        c.setCurrentTableName(head.getTableName());
        c.setPaginationFlag(head.getIsPage());
        c.setCheckboxFlag(head.getIsCheckbox());
        c.setScrollFlag(head.getScroll());

        c.setColumns(arrayList1);
        c.setDictOptions(hashMap);


        c.setFieldHrefSlots(arrayList2);


        c.setForeignKeys(arrayList3);


        c.setHideColumns(list2);


        List<OnlCgformButton> list3 = this.onlCgformHeadService.queryButtonList(str, true);
        ArrayList arrayList5 = new ArrayList();
        for (OnlCgformButton onlCgformButton : list3) {
            if (list2.contains(onlCgformButton.getButtonCode())) {
                continue;
            }
            arrayList5.add(onlCgformButton);
        }
        c.setCgButtonList(arrayList5);


        OnlCgformEnhanceJs onlCgformEnhanceJs = this.onlCgformHeadService.queryEnhanceJs(str, "list");
        if (onlCgformEnhanceJs != null && oConvertUtils.isNotEmpty(onlCgformEnhanceJs.getCgJs())) {
            String str1 = EnhanceJsUtil.b(onlCgformEnhanceJs.getCgJs(), list3);
            c.setEnhanceJs(str1);
        }


        if ("Y".equals(head.getIsTree())) {
            c.setPidField(head.getTreeParentIdField());
            c.setHasChildrenField(head.getTreeIdField());
            c.setTextField(head.getTreeFieldname());
        }

        return c;
    }

    private void a(List<OnlCgformField> paramList1, List<String> paramList2, List<OnlColumn> paramList3, String paramString1, String paramString2) {
        if (oConvertUtils.isNotEmpty(paramString2)) {
            String[] arrayOfString = paramString2.split(",");
            for (String str : arrayOfString) {
                for (OnlCgformField onlCgformField : paramList1) {
                    String str1 = onlCgformField.getDbFieldName();

                    if (1 != onlCgformField.getIsShowList().intValue()) {
                        continue;
                    }
                    if (str.equals(str1)) {
                        paramList2.add(str);
                        OnlColumn b = new OnlColumn(onlCgformField.getDbFieldTxt(), str1);
                        b.setCustomRender(paramString1);
                        paramList3.add(b);
                        break;
                    }
                }
            }
        }
    }


    @Override
    public JSONObject queryOnlineFormObj(OnlCgformHead head, OnlCgformEnhanceJs onlCgformEnhanceJs) {
        JSONObject jSONObject1 = new JSONObject();
        String str = head.getId();

        List list1 = this.onlCgformFieldService.queryAvailableFields(head.getId(), head.getTableName(), null, false);

        List list2 = this.onlCgformFieldService.queryDisabledFields(head.getTableName());

        EnhanceJsUtil.a(onlCgformEnhanceJs, head.getTableName(), list1);

        Field f = null;
        if ("Y".equals(head.getIsTree())) {
            f = new Field();
            f.setCodeField("id");
            f.setFieldName(head.getTreeParentIdField());
            f.setPidField(head.getTreeParentIdField());
            f.setPidValue("0");
            f.setHsaChildField(head.getTreeIdField());
            f.setTableName(org.dfzt.modules.online.cgform.util.b.f(head.getTableName()));
            f.setTextField(head.getTreeFieldname());
        }

        JSONObject jSONObject2 = org.dfzt.modules.online.cgform.util.b.a(list1, list2, f);
        jSONObject2.put("table", head.getTableName());

        jSONObject1.put("schema", jSONObject2);

        jSONObject1.put("head", head);


        List list3 = this.onlCgformHeadService.queryButtonList(str, false);
        if (list3 != null && list3.size() > 0) {
            jSONObject1.put("cgButtonList", list3);
        }

        if (onlCgformEnhanceJs != null && oConvertUtils.isNotEmpty(onlCgformEnhanceJs.getCgJs())) {
            String str1 = EnhanceJsUtil.c(onlCgformEnhanceJs.getCgJs(), list3);
            onlCgformEnhanceJs.setCgJs(str1);
            jSONObject1.put("enhanceJs", EnhanceJsUtil.a(onlCgformEnhanceJs.getCgJs()));
        }
        return jSONObject1;
    }


    @Override
    public JSONObject queryOnlineFormObj(OnlCgformHead head) {
        OnlCgformEnhanceJs onlCgformEnhanceJs = this.onlCgformHeadService.queryEnhanceJs(head.getId(), "form");
        return queryOnlineFormObj(head, onlCgformEnhanceJs);
    }


    private List<OnlCgformField> a(String paramString) {
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, paramString);

        lambdaQueryWrapper.orderByAsc(OnlCgformField::getOrderNum);
        return this.onlCgformFieldService.list(lambdaQueryWrapper);
    }
}
